package com.sunday.electric.smack;

import com.sunday.electric.utils.SharePerferenceUtils;

import org.jivesoftware.smack.ConnectionConfiguration;
import org.jivesoftware.smack.packet.Presence;
import org.jivesoftware.smack.tcp.XMPPTCPConnection;
import org.jivesoftware.smack.tcp.XMPPTCPConnectionConfiguration;

public class SmackImpl implements Smack {

    private static final int PACKET_TIMEOUT = 30000;// 超时时间
    private static SmackImpl smackImp;
    private static XMPPTCPConnection mXMPPConnection;

    public static SmackImpl getIns() {

        if (smackImp == null) {
            XMPPTCPConnectionConfiguration.Builder builder = XMPPTCPConnectionConfiguration.builder();
            builder.setSecurityMode(ConnectionConfiguration.SecurityMode.disabled);
            builder.setDebuggerEnabled(false);
            //报错
//            SASLAuthentication.blacklistSASLMechanism("SCRAM-SHA-1");
//            SASLAuthentication.blacklistSASLMechanism("DIGEST-MD5");
//			SASLAuthentication.blacklistSASLMechanism("PLAIN");


            XMPPTCPConnectionConfiguration config = builder.setServiceName(SmackConstants.SERVER_NAME)
                    .setHost(SmackConstants.HOST).setPort(1080)
                    .setUsernameAndPassword(SharePerferenceUtils.getIns().getUserId(),
                            "123456")
                    .build();

            mXMPPConnection = new XMPPTCPConnection(config);
            smackImp = new SmackImpl();
        }
        return smackImp;
    }

    public static String handlerUserName(String username) {
        String tempStr;
        String hostName = "@".concat(SmackConstants.SERVER_NAME).concat("/Spark");
        String hostName1 = "@".concat(SmackConstants.SERVER_NAME);
        String hostinIp = "@".concat(SmackConstants.SERVER_inIP).concat("/Spark");
        String hostinIp1 = "@".concat(SmackConstants.SERVER_inIP);
        String ahostName = "@".concat(SmackConstants.SERVER_NAME).concat("/Smack");
        String ahostinIp = "@".concat(SmackConstants.SERVER_inIP).concat("/Smack");
        int id = username.indexOf(hostName);
        int id1 = username.indexOf(hostName1);
        int ip = username.indexOf(hostinIp);
        int ip1 = username.indexOf(hostinIp1);
        int aip = username.indexOf(ahostName);
        int aip1 = username.indexOf(ahostinIp);
        if (id != -1) {
            tempStr = username.substring(0, id);
            return tempStr;
        } else if (id1 != -1) {
            tempStr = username.substring(0, id1);
            return tempStr;
        } else if (ip != -1) {
            tempStr = username.substring(0, ip);
            return tempStr;
        } else if (ip1 != -1) {
            tempStr = username.substring(0, ip1);
            return tempStr;
        } else if (aip != -1) {
            tempStr = username.substring(0, aip);
            return tempStr;
        } else if (aip1 != -1) {
            tempStr = username.substring(0, aip1);
            return tempStr;
        }
        return username;
    }

	/*public static SmackImpl  getIns(){
        if(smackImp==null){
			XMPPTCPConnectionConfiguration.Builder builder = XMPPTCPConnectionConfiguration.builder();
			builder.setSecurityMode(ConnectionConfiguration.SecurityMode.disabled);
			builder.setDebuggerEnabled(true);
			XMPPTCPConnectionConfiguration config = builder.setServiceName(SmackConstants.SERVER_NAME)
					.setHost(SmackConstants.HOST).setPort(5222)
					.setUsernameAndPassword(SharePerferenceUtils.getIns().getUserId(),
							SharePerferenceUtils.getIns().getOAuth().getPassword())
					.build();
			mXMPPConnection = new XMPPTCPConnection(config);
			smackImp = new SmackImpl();
		}
		return smackImp;
	}*/

    public XMPPTCPConnection getXMPPConnection() {
        return mXMPPConnection;
    }

    public void setSmackImpNull() {
        smackImp = null;
    }

    @Override
    public boolean login() {
        Presence offlinePres = new Presence(Presence.Type.unavailable, "", 1, Presence.Mode.away);

        if (mXMPPConnection.isConnected()) {
            try {
                mXMPPConnection.disconnect(offlinePres);
            } catch (Exception e) {
                e.printStackTrace();
                mXMPPConnection.disconnect();
            }
        }
        mXMPPConnection.setPacketReplyTimeout(PACKET_TIMEOUT);// 设置超时时间
        try {
            mXMPPConnection.connect();
            mXMPPConnection.login();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return mXMPPConnection.isAuthenticated();
    }

    public boolean isAuthenticated() {// 是否与服务器连接上，供本类和外部服务调用
        if (mXMPPConnection != null) {
            return (mXMPPConnection.isConnected() && mXMPPConnection
                    .isAuthenticated());
        }
        return false;
    }


    public void loginOut() {
       /* if (mXMPPConnection != null) {
            Presence offlinePres = new Presence(Presence.Type.unavailable, "", 1, Presence.Mode.away);
            try {
                mXMPPConnection.disconnect(offlinePres);
            } catch (Exception e) {
                e.printStackTrace();
                mXMPPConnection.disconnect();
            }
        }*/
    }


}
